<template>
  <el-cascader ref="orgCascader" v-model="orgCodeModel" :options="orgData" :props="orgProps" clearable @change="handleOrg" />
</template>

<script>
// 公共机构
import { commonOrg } from '@/api/common';
export default {
  data() {
    return {
      orgData: [],
      orgProps: {
        expandTrigger: 'hover',
        value: 'code',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    };
  },
  computed: {
    orgCodeModel: {
      get() {
        return this.orgCode;
      },
      set(v) {
        this.$emit('update:orgCode', v); // :orgCode.sync=""
      }
    }
  },
  created() {
  //  this.getOrgData()
  },
  methods: {
    getOrgData() {
      const commonOrgData = window.commonOrgData;
      if (commonOrgData.length) {
        this.orgData = commonOrgData;
      } else {
        commonOrg({
          code: '', // 地区编码
          type: 1 // 1-提取宁波/湖州市本级机构 2-不提取 (不传默认为1)
        }).then(({ success, value }) => {
          if (success && value) {
            this.orgData = value;
            window.commonOrgData = value;
          }
        });
      }
    },
    handleOrg(e) {
      this.$refs.orgCascader.dropDownVisible = false;
      const nodes = this.$refs.orgCascader.getCheckedNodes()[0];
      const label = nodes?.pathLabels;
      this.$emit('handleOrg', { value: e[e.length - 1], label });
    },
    handleReset() {
      this.orgCodeModel = [];
    }
  }
};
</script>

<style lang="scss">
.el-cascader-panel .el-radio {
  width: 100%;
  height: 100%;
  z-index: 10;
  position: absolute;
  top: 10px;
  right: 10px;
}

.el-cascader-panel .el-radio__input {
  visibility: hidden;
}

.el-cascader-panel .el-cascader-node__postfix {
  top: 10px;
}
</style>
